<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace BI\Controller;

/**
 * Description of ViewDataController
 *
 * @author Administrator
 */
class ViewDataController extends TransParamController{
    private $headerArray;
    /**
     * @方法：根据提交的参数，返回预览数据
     */
    public function getViewDataResults(){
        if($this->xNums + $this->yNums + $this->vNums ==0 ){
            $resultArr=$this->getEmptyResult();
        }else{
            switch ($this->type){
                case 1://排行榜基础数据展示
                    $resultArr = $this->getViewTopData();
                    break; 
                case 2://同比及环比基础表展示

                    break;
                case 3://带供应量的基础表展示
                    $resultArr = $this->getViewPivotData();
                    break;
                case 0://交叉分析基础表展示
                default :
                    $resultArr = $this->getViewPivotData();
                    break;
            }
            
        }
        return $resultArr;
    }
    /**
     * 返回空表格
     */
    private function getEmptyResult(){
        return array('h'=>array(),'r'=>array('i'=>array()));
    }
    
    /**
     * 方法：获取预览数据
     * 功能：根据选配传入的结果值，返回预览数据
     */
    private function getViewTopData(){
        $data = $this->getNotXViewTopicData();
        
        unset($this->headerArray);
        //var_dump($data);
        return $data;
    }
    
    /**
     * 方法：获取预览数据
     * 功能：根据选配传入的结果值，返回预览数据
     */
    private function getViewPivotData(){
        if($this->xNums > 0){//存在横向维度时
            if($this->yNums > 0){//同时存在纵向维度
                $data = $this->getHaveXViewPivotData();
            }else{//不存在纵向维度
                $data = $this->getSelectViewPivotData();
            }
        }else{//不存在横向维度时
            $data = $this->getNotXViewPivotData();
        }
        
        if($this->count['isYSum']==1){
            $data['f']=$this->getViewFootData();
        }
        unset($this->headerArray);
        //var_dump($data);
        return $data;
    }
    
    /**
     * @方法：处理同时存在XYV维度时数据
     */
    private function getHaveXViewPivotData(){
        //1.横维度
        $i=0;
        foreach ($this->xAxis as $k=>$key){
            $x[$i] = $this->setViewData($key);
            ++$i;
        }

        //2.纵维度
        $j=0;
        foreach ($this->yAxis as $k=>$key){
            $index[$j]=$key;
            $y[$j] = $this->setViewData($key);
            ++$j;
        }
        
        //2.统计维度
        $head = $this->setCountHeader();
        //$this->headData['RE'][$i][]=$this->setHeadOutFormat($total[1][0],'','',$colspan);
        $k=0;
        //将统计字段追加到表头数组
        foreach ($head as $val){
            $x[$i][$k]=$val;
            ++$k;
        }

        $res['h'] = $this->setHeaderViewData($x);
        $res['r'] = $this->setRowsViewData($y,$index);
        
        //unset($this->headerArray);//setCountHeader方法会生成此变量，清除
        return $res;
    }
    
    /**
     * @方法：存在X维度，不存在Y维度
     */
    private function getSelectViewPivotData(){
        //1.横维度
        $i=0;
        foreach ($this->xAxis as $key){
            $x[$i] = $this->setViewData($key);
            ++$i;
        }
        
        //2.当无纵向维度时，将统计值设计为纵向维度
        if($this->vNums > 0){
            $index[0]='yAxis';
            $val = $this->setCountHeader();
            foreach ($val as $v){
                $y[0][]=$v;
            }
            
            
            $res['r'] = $this->setRowsViewData($y,$index);
        }
        
        $res['h'] = $this->setHeaderViewData($x);
        
        //unset($this->headerArray);//上个方法会生成此变量，清除
        
        return $res;
    }
    
    /**
     * @方法：处理不存在X维度时数据
     * 参数：无X维度
     */
    private function getNotXViewPivotData(){
        $res=array();
        
        //1.横维度为0，则V维度将为表头
        if($this->vNums > 0){//存在统计值
            //2.统计维度
            $head = $this->setCountHeader();
            unset($this->headerArray);//上个方法会生成此变量，清除

            $k=0;
            //将统计字段追加到表头数组
            foreach ($head as $val){
                $x[0][$k]=$val;
                ++$k;
            }

            $res['h'] = $this->setHeaderViewData($x);
        }
        
        //2.纵维度
        if($this->yNums > 0){
            $j=0;
            foreach ($this->yAxis as $k=>$key){
                $index[$j]=$key;
                $y[$j] = $this->setViewData($key);
                
                if($this->type == 1){//排名
                    $line[]=array('t'=>$j+1,'f'=>'rank');
                }
                $line[]=array('t'=>$this->rulesArr[$key]['FieldName'],'f'=>$key);
                
                ++$j;
            }
            
            //显示表头
            if($this->vNums == 0){
                $res['h']['i']=array('0'=>$line);
                $res['h']['r'] = $this->getFixedColumn();
            }
            
            //显示左侧数据
            $res['r'] = $this->setRowsViewData($y,$index);
            
        }
        return $res;
    }
    
    /**
     * @方法：处理不存在排名数据的预览
     * @参数：无X维度
     */
    private function getNotXViewTopicData(){
        $res=array();
        
        //1.横维度为0，则V维度将为表头
        if($this->vNums > 0){//存在统计值
            //2.统计维度
            $head = $this->setCountHeader();
            unset($this->headerArray);//上个方法会生成此变量，清除

            $k=0;
            //将统计字段追加到表头数组
            foreach ($head as $val){
                $x[0][$k]=$val;
                ++$k;
            }

            $res['h'] = $this->setHeaderViewData($x);
        }
        
        //2.纵维度
        if($this->yNums > 0){
            $j=0;
            foreach ($this->yAxis as $k=>$key){
                $index[$j]=$key;
                $y[$j] = $this->setViewData($key);
                
                if($this->type == 1){//排名
                    if($this->yNums==1){
                        $line[]=array('t'=>$j+1,'f'=>'rank');
                    }                    
                }
                $line[]=array('t'=>$this->rulesArr[$key]['FieldName'],'f'=>$key);
                
                ++$j;
            }
            
            //显示表头
            if($this->vNums == 0){
                $res['h']['i']=array('0'=>$line);
                $res['h']['r'] = $this->getFixedColumn();
            }
            
            //显示左侧数据
            $res['r'] = $this->setRowsViewData($y,$index);
            
        }
        return $res;
    }
    
    /**
     * @方法：获取预览的纵统计值
     */
    private function getViewFootData(){
        
        $yArr=$this->getMergeHeadColumn();
        
        if($this->yNums>0){
            $line[]=array('t'=>'合计','f'=>$yArr[0],'c'=>$this->yNums);
        }else{
            $line[]=array('t'=>'合计','f'=>'yAxis');
        }
        
        foreach ($this->headerArray['FT'] as $key=>$val){
            $line[]=array('t'=>'','f'=>$val);
        }
        
        $item[] = $line;
        
        $res['i'] = $item;
        $res['h'] = $this->getColorColumn();
        //$res['f'] = $this->getFixedColumn();
        
        
        return $res;
    }
    
    /**
     * @方法：预览时预置数据
     * @参数：标准值
     * @返回：数组
     */
    private function setBaseData($key,$model){
        if($model==1){//固定数据
            switch ($key){
                case 'QY':
                    $data=array('渝中区','江北区','渝北区','九龙坡区','南岸区');
                    break;
                case 'ZT':
                    $data=array('茶园','照母山','大学城','空港新城','北碚老城');
                    break;
                case 'KFS':
                    $data=array('龙湖','金科','万科','中海','华润');
                    break;
                case 'HX':
                    $data=array('一房','二房','二房半','三房','三房半');
                    break;
                case 'LPMC':
                    $data=array('龙湖原著','金科十年城','万科悦峰','保利爱尚里','协信星光');
                    break;
                case 'YT':
                    $data=array('高层','洋房','别墅','商铺','车库');
                    break;
                case 'XFYT':
                    $data=array('平层','跃层','公寓','联排别墅','商铺');
                    break;
            }
        }else{//数据库取数
            $sql='select top 5 '.$this->rulesArr[$key]['ContrastName'].' from '.$this->rulesArr[$key]['ContrastSheet'].' order by '.$this->rulesArr[$key]['ContrastSort'];
            $d = M()->query($sql);
            if(is_array($d)){
                foreach ($d as $v){
                    $data[]=$v[$this->rulesArr[$key]['ContrastName']];
                }
            }
        }
        
        return $data;
    }

        /**
     * @方法：获取预览数据源
     * @参数：键值，数值
     */
    private function setViewData($key){
        switch ($this->rulesArr[$key]['FieldType']){
            case 1://对照值时
                $data = F('View/'.$key);
                if(empty($data)){
                    $data = $this->setBaseData($key,1);
                    F('View/'.$key,$data);
                }
                break;
            case 2://区间值
                unset($this->region);
                $this->setRegionCaseWhen($key,$this->regionColumn[$key]);//获取区间段，可返回$this->region
                foreach ($this->region[$key] as $val){
                    $data[]=$val['title'];
                }
                break;
            case 3://日期
                $exp = $this->rulesArr[$key]['Expressions'];
                $k = explode('|', $exp);
                
                switch ($k[0]){
                    case 'Y'://年份
                        for($Y=2010;$Y<=date('Y');$Y++){
                            $data[]=$Y.'年';
                        }
                        break;
                    case 'YM':
                        for($YM=1;$YM<=6;$YM++){
                            $data[]=date('Y').'年'.$YM.'月';
                        }
                        break;
                    case 'YQ':
                        for($YQ=1;$YQ<=4;$YQ++){
                            $data[]=date('Y').'年'.$YQ.'季';
                        }
                        break; 
                    case 'YW':
                        for($YW=1;$YW<=5;$YW++){
                            $data[]=date('Y').'年'.$YW.'周';
                        }
                        break;
                    case 'YB':
                        $data=array('2012年上半年','2012年下半年','2013年上半年','2013年下半年','2014年上半年');
                        break;
                    case 'D':
                        for($D=date('d')-5;$D<=date('d');$D++){
                            $data[]=date('Y').'年'.date('m').'月'.$D.'日';
                        }
                        break;
                }
                break;
            case 4://原值
                $data=array('A组团','B组团','C组团','第一期','第二期');
                break;
        }
        return $data;
    }
 
    /**
     * @方法：解析表头预览数据
     */
    private function setHeaderViewData($head){
        //1.计算总层级数量
        $nums=count($head);
        //2.计算每一层级数量
        $n=array();
        for($i=0;$i<$nums;$i++){
            $n[$i]=count($head[$i]);
        }
        
        //3.计算每一层级共占列数
        for($j=0;$j<$nums;$j++){
            $count=1;
            for($k=$j+1;$k<$nums;$k++){
                $count *= $n[$k];
            }
            $c[$j]=$count;
        }
        
        //4.计算每一级需要重复的次数
        for($j=0;$j<$nums;$j++){
            $count=1;
            for($k=0;$k<$j;$k++){
                $count *= $n[$k];
            }
            $p[$j]=$count;
        }
        
        //6.计算每一数据的合并值
        $f=1;
        foreach ($head as $key=>$val){
            $line=array();
            $array=array();
            
            //5.追加左边标题列
            if($this->type == 1){
                $line[]=array('t'=>'排名','f'=>'rank');
            }
            
            if($this->yNums > 0){//存在Y维度
                foreach ($this->yAxis as $k){
                    $line[]=array('t'=>$this->rulesArr[$k]['FieldName'],'f'=>$k);
                }
            }else{
                if($this->xNums > 0 and $this->vNums >0 ){
                    $line[]=array('t'=>'统计方式','f'=>'yAxis');
                }
            }
        
            for($j=0;$j<$p[$key];$j++){//重复次数
                foreach ($val as $v){
                    if($c[$key]>1){
                        $line[]=array('t'=>$v,'f'=>'','c'=>$c[$key]);
                    }else{
                        $line[]=array('t'=>$v,'f'=>'C'.$f);
                        $array[]='C'.$f;
                        ++$f;
                    }
                }
            }
            
            if($this->count['isXSum'] == 1){
               $line[]=array('t'=>'合计','f'=>'SUM');
               $array[]='SUM';
            }
  
            $item[]=$line;
        }
        
        //用于输入FOOT
        $this->headerArray['FT']=$array;
        
        $res['i'] = $item;
        $res['r'] = $this->getFixedColumn();
        return $res;
    }
    
    /**
     * 方法：根据纵向维度，解析出纵向值
     */
    private function setRowsViewData($rows,$index){
        //var_dump($rows);var_dump($index);
        //1.计算总列数
        $nums=count($rows);
        
        $no=1;
        if($nums==1){
            foreach ($rows[0] as $key0=>$val0){
                if($this->type == 1){
                    if($no <= $this->ranking['topNum']){
                        $data[]=array('rank'=>$no,$index[0]=>$val0);
                    }
                    ++$no;
                }else{
                    $data[]=array($index[0]=>$val0);
                }
            }
        }elseif($nums==2){
            foreach ($rows[0] as $key0=>$val0){
                foreach ($rows[1] as $key1=>$val1){
                    if($this->type == 1){
                        if($no<=$this->ranking['topNum']){
                            $data[]=array('rank'=>$no,$index[0]=>$val0,$index[1]=>$val1);
                        }
                        ++$no;
                    }else{
                        $data[]=array($index[0]=>$val0,$index[1]=>$val1);
                    }
                }
            }
            
        }elseif($nums==3){
            foreach ($rows[0] as $key0=>$val0){
                foreach ($rows[1] as $key1=>$val1){
                    foreach ($rows[2] as $key2=>$val2){
                        if($this->type == 1){
                            if($no<=$this->ranking['topNum']){
                                $data[]=array('rank'=>$no,$index[0]=>$val0,$index[1]=>$val1,$index[2]=>$val2);
                            }
                            ++$no;
                        }else{
                            $data[]=array($index[0]=>$val0,$index[1]=>$val1,$index[2]=>$val2);
                        }
                    }  
                }                
            }            
        }elseif($nums==4){
            foreach ($rows[0] as $key0=>$val0){
                foreach ($rows[1] as $key1=>$val1){
                    foreach ($rows[2] as $key2=>$val2){
                        foreach ($rows[3] as $key3=>$val3){
                            if($this->type == 1){
                                if($no<=$this->ranking['topNum']){
                                    $data[]=array('rank'=>$no,$index[0]=>$val0,$index[1]=>$val1,$index[2]=>$val2,$index[3]=>$val3);
                                }
                                ++$no;
                            }else{
                                $data[]=array($index[0]=>$val0,$index[1]=>$val1,$index[2]=>$val2,$index[3]=>$val3);
                            }
                        }
                    }  
                }                
            }            
        }
        elseif($nums==5){
            foreach ($rows[0] as $key0=>$val0){
                foreach ($rows[1] as $key1=>$val1){
                    foreach ($rows[2] as $key2=>$val2){
                        foreach ($rows[3] as $key3=>$val3){
                            foreach ($rows[4] as $key4=>$val4){
                                if($this->type == 1){
                                    if($no<=$this->ranking['topNum']){
                                        $data[]=array('rank'=>$no,$index[0]=>$val0,$index[1]=>$val1,$index[2]=>$val2,$index[3]=>$val3,$index[4]=>$val4);
                                    }
                                    ++$no;
                                }else{
                                    $data[]=array($index[0]=>$val0,$index[1]=>$val1,$index[2]=>$val2,$index[3]=>$val3,$index[4]=>$val4);
                                }
                            }
                        }
                    }
                }                
            }            
        }
        
        $res['i'] = $data;
        $res['r'] = $this->getMergeColumn();
        $res['h'] = $this->getColorColumn();
        //$res['f'] = $this->getFixedColumn();

        return $res;
    }
}
